package com.gis.visualization.service.impl;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gis.visualization.config.exceptionhandler.VisException;
import com.gis.visualization.entity.FigureLocation;
import com.gis.visualization.entity.FigureLocationDesc;
import com.gis.visualization.entity.vo.FigureLocationVo;
import com.gis.visualization.mapper.FigureLocationMapper;
import com.gis.visualization.service.FigureLocationDescService;
import com.gis.visualization.service.FigureLocationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 人物地点表 服务实现类
 * </p>
 *
 * @author hqz
 * @since 2021-02-24
 */
@Service
public class FigureLocationServiceImpl extends ServiceImpl<FigureLocationMapper, FigureLocation> implements FigureLocationService {

    @Autowired
    private FigureLocationDescService locationDescService;

    @Override
    public String addFigureLocation(FigureLocationVo figureLocationVo) {
//        存储地点信息
        FigureLocation figureLocation = new FigureLocation();
        BeanUtils.copyProperties(figureLocationVo, figureLocation);
        baseMapper.insert(figureLocation);

        String locationId = figureLocation.getId();
//        存储地点描述
        FigureLocationDesc locationDesc = new FigureLocationDesc();
//        地点与地点描述一对一
        locationDesc.setId(locationId);
        locationDesc.setDescription(figureLocationVo.getDescription());
        locationDescService.save(locationDesc);

        return locationId;
    }

    @Override
    public List<FigureLocationVo> getFigureLocation(String figureId) {
//        根据人物id 查询人物的多个地点
        QueryWrapper<FigureLocation> locationQueryWrapper = new QueryWrapper<>();
        locationQueryWrapper.eq("figure_id", figureId);
        List<FigureLocation> locationList = baseMapper.selectList(locationQueryWrapper);

        List<FigureLocationVo> figureLocationVoList = new ArrayList<>();

        if (locationList != null) {
            locationList.forEach(item -> {
                FigureLocationVo figureLocationVo = new FigureLocationVo();
//            赋值
                BeanUtils.copyProperties(item, figureLocationVo);
//            通过地点id查询地点描述
                FigureLocationDesc locationDesc = locationDescService.getById(item.getId());
//            赋值描述信息给封装对象
                figureLocationVo.setDescription(locationDesc.getDescription());
//            添加到集合
                figureLocationVoList.add(figureLocationVo);
            });
        }


        return figureLocationVoList;
    }

    @Override
    public void updateFigureLocation(FigureLocationVo figureLocationVo) {
        if (figureLocationVo == null) {
            throw new VisException(20001, "提交对象为空");
        }
        FigureLocation figureLocation = new FigureLocation();
        BeanUtils.copyProperties(figureLocationVo, figureLocation);
//        更新地点信息
        baseMapper.updateById(figureLocation);

        FigureLocationDesc locationDesc = new FigureLocationDesc();
        locationDesc.setId(figureLocationVo.getId());
        locationDesc.setDescription(figureLocationVo.getDescription());
//        更新地点描述
        locationDescService.updateById(locationDesc);
    }

    @Override
    public void deleteFigureLocation(String locationId) {
        baseMapper.deleteById(locationId);
        locationDescService.removeById(locationId);
    }

    @Override
    public Map<String, Object> getPageFigureLocation(String figureId, long current, long limit) {
        //获取历史人物的地点信息
        LambdaQueryWrapper<FigureLocation> locations = new LambdaQueryWrapper<>();
        //根据人物id查他的地点
        locations.eq(FigureLocation::getFigureId, figureId);
        //根据地点时间排序
        locations.orderByAsc(FigureLocation::getSort);
        Integer total = baseMapper.selectCount(locations);
        //限制取的数量个
        locations.last("limit " + ((current-1)*limit) + "," + limit);
        //获取查询结果
        List<Map<String, Object>> maps = baseMapper.selectMaps(locations);
        //遍历 查询 地点描述 再封装
        FigureLocationDesc desc = null;
        for (Map<String, Object> map : maps) {
            desc = locationDescService.getById(map.get("id").toString());
            map.put("description", desc.getDescription());
        }
        Map<String,Object> pageMap = MapUtil.newHashMap();
        pageMap.put("locationData",maps);
        pageMap.put("total",total);
        return pageMap;
    }

    @Override
    public void deleteLocationByFigureId(String figureId) {
        LambdaQueryWrapper<FigureLocation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FigureLocation::getFigureId,figureId);
        queryWrapper.select(FigureLocation::getId);
        List<FigureLocation> locationList = baseMapper.selectList(queryWrapper);
        List<String> idList = new ArrayList<>();
        for (FigureLocation f : locationList){
            idList.add(f.getId());
        }
        locationDescService.removeByIds(idList);
        baseMapper.delete(queryWrapper);
    }

    @Override
    public void deleteSelectLocations(List<String> locationIds) {
        locationDescService.removeByIds(locationIds);
        baseMapper.deleteBatchIds(locationIds);
    }
}
